package com.ruoyi.hr.service.impl.changemanage.internshipchange;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.hr.domain.Authority;
import com.ruoyi.hr.mapper.SkyMsgAlarmMapper;
import com.ruoyi.hr.mapper.changemanage.ChangeManageMapper;
import com.ruoyi.hr.mapper.changemanage.internshipchange.InternshipChangeMapper;
import com.ruoyi.hr.service.changemanage.internshipchange.InternshipChangeService;
import com.ruoyi.hr.util.BaseResult;
import com.ruoyi.hr.util.ResultEnum;
import com.ruoyi.hr.util.utils;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class InternshipChangeServiceImpl implements InternshipChangeService {

    @Autowired
    InternshipChangeMapper internshipChangeMapper;

    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    SkyMsgAlarmMapper skyMsgAlarmMapper;

    @Autowired
    ChangeManageMapper changeManageMapper;

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> findInternshipChangeListData(Map map, Authority authority) {
        List<Map<String, Object>> maps = internshipChangeMapper.findInternshipChangeListData(map, authority);
        return maps;
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> findInternshipChangeEmp(Map map, Authority authority) {
        return internshipChangeMapper.findInternshipChangeEmp(map, authority);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public BaseResult addInternshipChangeEmp(String allEid) {
        //叠加异常信息
        String msgAdd = "";
        try {
            //存储过程sql
            String sql;
            SysUser user = ShiroUtils.getSysUser();
            Map<String, Object> maps = sysUserMapper.selectSkySecUser(user.getLoginName());
            String[] array = allEid.split(",");
            String msg;//异常信息
            for (String s : array) {
                //根据PEID取出人名
                String name = changeManageMapper.getEmpName(s);
                sql = "declare @RetVal varchar(50) exec eSP_PracAddEmp " + s + ",1," + maps.get("id").toString() + ",@RetVal output select @RetVal";
                String retVal = internshipChangeMapper.executeProcedure(sql);
                msg = skyMsgAlarmMapper.getMsgById(retVal);
                if (msg != null) {
                    msgAdd += name + ":" + msg + "\n";
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return BaseResult.build(ResultEnum.SERVER_ERROR);
        }
        if ("".equals(msgAdd)) {
            return BaseResult.build(ResultEnum.SUCCESS);
        } else {
            return BaseResult.build(19970926, msgAdd, null);
        }
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public Map<String, Object> getInternshipEmpById(String id) {
        Map<String, Object> InternshipEmp = internshipChangeMapper.getInternshipEmpById(id);
        return InternshipEmp;
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public int editInternshipEmp(Map map) {
        return internshipChangeMapper.editInternshipEmp(map);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public BaseResult procedure(String allId, String procedure) {
        //叠加异常信息
        String msgAdd = "";
        try {
            //存储过程sql
            String sql = "";
            SysUser user = ShiroUtils.getSysUser();
            Map<String, Object> maps = sysUserMapper.selectSkySecUser(user.getLoginName());
            String[] array = allId.split(",");
            String msg;//异常信息
            for (String id : array) {
                //根据id取出人名
                String name = internshipChangeMapper.findNameByEid(id);
                if (procedure.equals("eSP_PracCheck")) {
                    sql = "declare @RetVal varchar(50) exec eSP_PracCheck " + id + "," + maps.get("id").toString() + ",@RetVal output select @RetVal";
                } else if (procedure.equals("eSP_PracCancel")) {
                    sql = "declare @RetVal varchar(50) exec eSP_PracCancel " + id + "," + maps.get("id").toString() + ",@RetVal output select @RetVal";
                } else if (procedure.equals("eSP_PracStart")) {
                    sql = "declare @RetVal varchar(50) exec eSP_PracStart " + id + "," + maps.get("id").toString() + ",@RetVal output select @RetVal";
                }
                msg = "";
                List<List<Map<String, Object>>> retVal = internshipChangeMapper.ExProcedure(sql);
                //如果大小为1直接取出返回值，先判断是否有key,再判断是否为数字执行
                if (retVal.size() == 1) {
                    if (((Map) retVal.get(0)).containsKey("")) {
                        String by = ((Map) retVal.get(0)).get("").toString();
                        if (utils.isInteger(by)) {
                            msg = skyMsgAlarmMapper.getMsgById(((Map) retVal.get(0)).get("").toString());
                        }
                    }
                } else {
                    for (List<Map<String, Object>> list : retVal) {
                        for (Map<String, Object> map : list) {
                            if (map.containsKey("")) {
                                String by = map.get("").toString();
                                if (utils.isInteger(by)) {
                                    msg += skyMsgAlarmMapper.getMsgById(map.get("").toString());
                                }
                            }
                        }
                    }
                }
                if (msg != null && !msg.equals("") && !msg.equals("null")) {
                    msgAdd += name + ":" + msg + "\n";
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return BaseResult.build(ResultEnum.SERVER_ERROR);
        }
        if ("".equals(msgAdd)) {

            if (procedure.equals("eSP_PracCheck")) {
                return BaseResult.build(ResultEnum.SERVER_ERROR, "数据确认成功!");
            } else if (procedure.equals("eSP_PracCancel")) {
                return BaseResult.build(ResultEnum.SERVER_ERROR, "取消确认成功!");
            } else if (procedure.equals("eSP_PracStart")) {
                return BaseResult.build(ResultEnum.SERVER_ERROR, "执行变动成功!");
            }
            return BaseResult.build(ResultEnum.SUCCESS);
        } else {
            return BaseResult.build(19970926, msgAdd, null);
        }
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public int removeData(String ids) {
        String[] idSplit = ids.split(",");
        int num = 0;
        for (String id : idSplit) {
            num += internshipChangeMapper.removeData(id);
        }
        return num;
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> historyList(Map map, Authority authority) {
        return internshipChangeMapper.historyList(map, authority);
    }
}
